from QQLoginTool.QQtool import OAuthQQ
from rest_framework.response import Response
from rest_framework.views import APIView
from django.conf import settings
from rest_framework.generics import GenericAPIView

from oauth import serializers
from oauth.utils import generate_sign_openid
from .models import OAuthQQUser


class OAuthQQLoginView(APIView):
    def get(self,request):
        #1,获取参数
        state = request.query_params.get("state","/")
        #这一段的　　　根目录是什么鬼？？？为什么获取不到就会这样

        #2,创建qq对象
        oauth_qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID,
                client_secret=settings.QQ_CLIENT_SECRET,
                redirect_uri=settings.QQ_REDIRECT_URI,
                state=state)
                #这个看不懂，创建对象都是固定的么？是的　　没有ＱＱ对象没法获得ＱＱ地址
        #3,返回qq登陆页面
        login_url = oauth_qq.get_qq_url()
        return Response({"auth_url":login_url})

class OAuthQQUserView(GenericAPIView):
    serializer_class = serializers.OAuthQQUserSerializer
    def get(self,request):
        #1,获取参数code,校验
        code = request.query_params.get("code")
        #怎么得到的code前端是怎么运行的
        if not code:
            return Response("code丢失了")

        #2,根据code获取access_token
        oauth_qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID,
                           client_secret=settings.QQ_CLIENT_SECRET,
                           redirect_uri=settings.QQ_REDIRECT_URI,
                           state="/")
        access_token = oauth_qq.get_access_token(code)


        #3,根据access_token获取openid
        openid = oauth_qq.get_open_id(access_token)
        print("openid = %s"%openid)
        #4,通过openid查询qq对象用户
        try:
            oauth_qq_user = OAuthQQUser.objects.get(openid=openid)
            #为什么获取不到数据库的openid对比？？？？　　　因为用的是get方法　　　而get方法如果数据库有两个的话会报错
        except OAuthQQUser.DoesNotExist:
            #4,1初次授权
            sign_openid = generate_sign_openid(openid)
            return Response({"access_token":sign_openid})
        else:
            #5,非初次授权
            user = oauth_qq_user.user
            #这看不懂

            from rest_framework_jwt.settings import api_settings
            jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
            jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)


            return Response({
                "token":token,
                "username":user.username,
                "user_id":user.id
            })

    def post(self,request):
        dict_data = request.data
        serializer = self.get_serializer(data=dict_data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        # 3,1根据用户数据,生成token
        #这一段一直不懂什么意思？？？？？？？？ＪＷＴ加密５行固定代码
        from rest_framework_jwt.settings import api_settings

        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)
        return Response(
            {
                "token":token,
                "user_id":user.id,
                "username":user.username
            }
        )
